//=============================================================================
// Fusion Gatling Gun
//=============================================================================
class TFFusionGatling extends TFWeapon
    config(user);

var     float           CurrentRoll;
var     float           RollSpeed;
var() vector            AttachLoc;
var() rotator           AttachRot;
var   int               CurrentMode;
var() float             Blend;
var() bool 		        bStayWound;


replication
{
	unreliable if (Role == ROLE_Authority)
	  bStayWound;
}

simulated function OutOfAmmo()
{
    if ( !Instigator.IsLocallyControlled() || HasAmmo())
        return;
    else{
	Instigator.AmbientSound = None;
	Instigator.GroundSpeed = Instigator.default.GroundSpeed;
	Instigator.SoundPitch = Instigator.default.SoundPitch;
	Instigator.SoundVolume = Instigator.default.SoundVolume;
	DoAutoSwitch();}
}

simulated function bool HasAmmo()
{
    return ((Ammo[0] != None && FireMode[0] != None && Ammo[0].AmmoAmount >= FireMode[0].AmmoPerFire));
}

function DropFrom(vector StartLocation)
{
    local int m;
	local Pickup Pickup;

    Instigator.GroundSpeed = Instigator.default.GroundSpeed;
    Instigator.SoundPitch = Instigator.default.SoundPitch;
    Instigator.SoundVolume = Instigator.default.SoundVolume;

    bStayWound = False;

    if (!bCanThrow || !HasAmmo())
        return;

    ClientWeaponThrown();

    for (m = 0; m < NUM_FIRE_MODES; m++)
    {
        if (FireMode[m].bIsFiring)
            StopFire(m);
    }

	if ( Instigator != None )
	{
		DetachFromPawn(Instigator);
	}

	Pickup = Spawn(PickupClass,,, StartLocation);
	if ( Pickup != None )
	{
    	Pickup.InitDroppedPickupFor(self);
	    Pickup.Velocity = Velocity;
        if (Instigator.Health > 0)
            WeaponPickup(Pickup).bThrown = true;
    }

    Destroy();
}

/* BotFire()
called by NPC firing weapon. Weapon chooses appropriate firing Mode to use (typically no change)
bFinished should only be true if called from the Finished() function
FiringMode can be passed in to specify a firing Mode (used by scripted sequences)
*/
function bool BotFire(bool bFinished, optional name FiringMode)
{
    local int newmode;
    local Controller C;

    C = Instigator.Controller;
	newMode = BestMode();

	if (Bot(Instigator.Controller) != None)
		bStayWound=True;

	if ( newMode == 0 )
	{
		C.bFire = 1;
		C.bAltFire = 0;
	}
	else
	{
		C.bFire = 0;
		C.bAltFire = 1;
	}

	if ( bFinished )
		return true;

    if ( FireMode[BotMode].bIsFiring && (NewMode != BotMode) )
		StopFire(BotMode);

    if ( !ReadyToFire(newMode) || ClientState != WS_ReadyToFire )
		return false;

    BotMode = NewMode;
    StartFire(NewMode);
    return true;
}

// AI Interface
function float GetAIRating()
{
	return AIRating * FMin(Pawn(Owner).DamageScaling, 1.5);
}

/* BestMode()
choose between regular or alt-fire
*/
function byte BestMode()
{
	return 0;
}
// end AI Interface

simulated function bool FirstPersonView()
{
    return (Instigator.IsLocallyControlled() && (PlayerController(Instigator.Controller) != None) && !PlayerController(Instigator.Controller).bBehindView);
}

// Prevents wrong anims from playing
simulated function AnimEnd(int channel)
{
}

// Client-side only: update the first person barrel rotation
simulated function UpdateRoll(float dt, float speed, int mode)
{
    local rotator r;

    if (Level.NetMode == NM_DedicatedServer)
        return;

    if (mode == CurrentMode) // to limit to one mode
    {
       // log(self$" updateroll (mode="$mode$") speed="$speed);

        RollSpeed = speed;
        CurrentRoll += dt*RollSpeed;
        CurrentRoll = CurrentRoll % 65536.f;
        r.Roll = int(CurrentRoll);
        SetBoneRotation('Bone_Barrel', r, 0, Blend);
    }
}

simulated function bool StartFire(int mode)
{
	local bool bStart;

	bStart = Super.StartFire(mode);
	if (bStart)
	{
	FireMode[Mode].StartFiring();
	}

	return bStart;
}

// Allow fire modes to return to idle on weapon switch (server)
simulated function DetachFromPawn(Pawn P)
{
    //log(self$" detach from pawn p="$p);

    ReturnToIdle();
    Super.DetachFromPawn(P);
}

// Allow fire modes to return to idle on weapon switch (client)
simulated function bool PutDown()
{
   // log(self$" putdown");

    ReturnToIdle();
    return Super.PutDown();
}

simulated function ReturnToIdle()
{
	if (FireMode[0] != None)
	{
		FusionGatlingFire( FireMode[0] ).StopRolling();
		FireMode[0].GotoState('Idle');
	}
}

defaultproperties
{
     AttachLoc=(X=-77.000000,Y=6.000000,Z=4.000000)
     AttachRot=(Pitch=22000,Yaw=-16384)
     Blend=1.000000
     FireModeClass(0)=Class'Taskforces.FusionGatlingFire'
     FireModeClass(1)=Class'Taskforces.FusionGatlingAltFire'
     SelectAnim="Pickup"
     PutDownAnim="PutDown"
     IdleAnimRate=0.500000
     SelectSound=Sound'WeaponSounds.Minigun.SwitchToMiniGun'
     SelectForce="SwitchToMiniGun"
     AIRating=0.710000
     CurrentRating=0.710000
     bNoAmmoInstances=False
     EffectOffset=(X=50.000000,Y=1.000000,Z=10.000000)
     DisplayFOV=70.000000
     Priority=12
     HudColor=(B=255)
     SmallViewOffset=(X=12.000000,Y=14.000000,Z=-6.000000)
     CenteredOffsetY=-5.000000
     CenteredYaw=-500
     InventoryGroup=6
     GroupOffset=2
     PickupClass=Class'Taskforces.FusionGatlingPickup'
     PlayerViewOffset=(Y=8.000000)
     PlayerViewPivot=(Yaw=500,Roll=1000)
     BobDamping=1.200000
     AttachmentClass=class'Taskforces.FusionGatlingAttachment'
     IconMaterial=Texture'TFMedia.General.ArkonHUD'
     IconCoords=(X1=253,Y1=143,X2=333,Y2=180)
     ItemName="Fusion Gatling"
     LightHue=30
     LightSaturation=150
     LightBrightness=150.000000
     LightRadius=4.000000
     LightPeriod=3
     Mesh=SkeletalMesh'Weapons.RocketLauncher_1st'
     Skins(0)=Combiner'TFMedia.Fusion.FusionGatlingCombiner1'
     UV2Texture=Shader'XGameShaders.WeaponShaders.WeaponEnvShader'
     SoundRadius=400.000000
     AmmoRegenAmount(0)=10
     SkillCost=400
     StationDmgModifier=3
}
